package com.example.administrator.szb.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.base.MVPBaseActivity;
import com.example.administrator.szb.bean.IndexBean;
import com.example.administrator.szb.bean.Messages;
import com.example.administrator.szb.fragments.fragment_forTab.home.adapter.JGadapter;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.JsonUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.QTLog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.OnRefreshLoadmoreListener;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by D N on 2017/11/15.
 */

public class AllJGActivity extends MVPBaseActivity {
    private ImageView right_img;
    private TextView bar_title;
    int current_page = 1 ;
    int per_page = 10;
    private SmartRefreshLayout freshlayout;
    private ListView listView;
    View error_net_ll,noda_ll;
    private LinearLayout ll_error;
    private ImageView iv_error;
    private TextView tv_net_error;
    private TextView tv_tips;
    private Button btn_error;
    private LinearLayout no_data_ll;
    private ImageView iv_no_data;
    private TextView tv_no_data;
    private TextView tv_tips2;
    private Button no_data_btn;
    private List list = new ArrayList();
    private boolean isFirstEnter = true;
    JGadapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.all_jg_activity);

    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        right_img = findViewById(R.id.right_img);
        right_img.setImageResource(R.drawable.search_bs);
        bar_title = findViewById(R.id.bar_title);
        bar_title.setText("全部机构");
        noda_ll = findViewById(R.id.noda_ll);
        error_net_ll = findViewById(R.id.error_net_ll);
        freshlayout = (SmartRefreshLayout) findViewById(R.id.freshlayout);
        listView = (ListView) findViewById(R.id.listView);
        listView.setBackgroundColor(Color.parseColor("#F3F3F3"));
        ll_error = (LinearLayout) findViewById(R.id.ll_error);
        iv_error = (ImageView) findViewById(R.id.iv_error);
        tv_net_error = (TextView) findViewById(R.id.tv_net_error);
        tv_tips = (TextView) findViewById(R.id.tv_tips);
        btn_error = (Button) findViewById(R.id.btn_error);
        no_data_ll = (LinearLayout) findViewById(R.id.no_data_ll);
        iv_no_data = (ImageView) findViewById(R.id.iv_no_data);
        tv_no_data = (TextView) findViewById(R.id.tv_no_data);
        tv_tips2 = (TextView) findViewById(R.id.tv_tips2);
        no_data_btn = (Button) findViewById(R.id.no_data_btn);
    }

    @Override
    protected void initEvent() {
        right_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,Search2Activity.class);
                startActivity(intent);
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context,JGXQActivity.class);
                intent.putExtra("id",((IndexBean.DataBean.HotCounBean)list.get(position)).getId());
                startActivity(intent);
            }
        });
        if (isFirstEnter) {
            isFirstEnter = false;
            freshlayout.autoRefresh();
        }
        freshlayout.setEnableLoadMore(true);//是否启用上拉加载功能
        freshlayout.setEnableLoadMoreWhenContentNotFull(true);//是否在列表不满一页时候开启上拉加载功能


        freshlayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                current_page++;
                requestData();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                current_page = 1;
                requestData();
            }
        });
        btn_error.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                freshlayout.autoRefresh();
                requestData();
            }
        });
    }

    @Override
    protected void initData() {
        adapter = new JGadapter(context,list);
        listView.setAdapter(adapter);
    }

    private void requestData(){
        HashMap<String,Object> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("current_page",current_page+"");
        map.put("per_page",per_page+"");
        String url = URLAddress.URLBASE +URLAddress.URL_MORE_JG;
        HttpUtils.post2(activity,url,map ,0,new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                str = str.replace("\"list\":{}","\"list\":[]");
                Messages messages = JsonUtils.getMessages2(str);

                if (messages.getResult()==1){
                    str = JsonUtils.JsonParse(messages);
                    ALlJG aLlJG = JSON.parseObject(str,new TypeReference<ALlJG>(){});
                    str = aLlJG.getList();

                    List l = JSON.parseArray(str, IndexBean.DataBean.HotCounBean.class);
                    if (current_page==1){
                        list.clear();
                        freshlayout.setEnableLoadMore(true);
                        if (l.size()<per_page){

                            //没有更多了
                            freshlayout.setEnableLoadMore(false);

                        }
                        freshlayout.finishRefresh();
                    }else {
                        freshlayout.finishLoadmore();
                        if (l.size()<per_page){
                            freshlayout.setEnableLoadMore(false);
                            //current_page--;
                        }
                    }
                    list.addAll(l);

                    if (list.size()==0){

                        showNodata();
                    }
                    showSuPei();
                }


            }

            @Override
            public void onFailure(int requestCode, String str) {
                freshlayout.finishLoadmore();
                freshlayout.finishRefresh();
                showError();
            }
        });
    }

    public void showSuPei(){
        adapter.notifyDataSetChanged();

    }
    private void showNodata(){
        freshlayout.setVisibility(View.VISIBLE);
        noda_ll.setVisibility(View.VISIBLE);
        listView.setVisibility(View.GONE);
        error_net_ll.setVisibility(View.GONE);
        QTLog.e("aaaaa");
    }
    private void hideAll(){
        noda_ll.setVisibility(View.GONE);
        freshlayout.setVisibility(View.VISIBLE);
        listView.setVisibility(View.VISIBLE);
        error_net_ll.setVisibility(View.GONE);
    }
    private void showError(){
        freshlayout.setVisibility(View.GONE);
        error_net_ll.setVisibility(View.VISIBLE);
    }

    public static class ALlJG{

        /**
         * current_page : 1
         * per_page : 10
         * list :
         */

        private String current_page;
        private String per_page;
        private String list;

        public String getCurrent_page() {
            return current_page;
        }

        public void setCurrent_page(String current_page) {
            this.current_page = current_page;
        }

        public String getPer_page() {
            return per_page;
        }

        public void setPer_page(String per_page) {
            this.per_page = per_page;
        }

        public String getList() {
            return list;
        }

        public void setList(String list) {
            this.list = list;
        }
    }
}
